package FlowGraph.Nodes;

import antlr.collections.AST;

import java.util.*;

import util.Line;
import FlowGraph.*;
import FlowGraph.StatementInfo.StatementInfo;
import FlowGraph.EdgeInfo.*;

//////////////////////////////////////////////////////////////////////////
//// WhileBeginNode
/**

@author Ivan Jeukens
@version v 0.1 03/10/2001
*/
public class WhileBeginNode extends FlowGraphNode implements LoopHeadNode {

    public WhileBeginNode(AST statement) {
        super(statement, WHILE_BEGIN, 1, 2);
        _condition = statement.getFirstChild();

        _statementInfo = new StatementInfo(this, _condition);
        _iteExtractedNodes = new LinkedList();
    }
    
    ///////////////////////////////////////////////////////////////////
    ////                        public methods                     ////

    public FlowGraphEdge getContinueEdge() {
        Iterator ite = _outputEdges.iterator();
        while(ite.hasNext()) {
            FlowGraphEdge edge = (FlowGraphEdge) ite.next();
            LoopEdge le = (LoopEdge) edge.getEdgeInfo();
            if(le.getType() == EdgeInfo.LOOP_BODY) {
                return edge;
            }
        }
        return null;
    }

    public FlowGraphNode getContinueNode() {
        FlowGraphEdge edge = getContinueEdge();
        if(edge != null) return edge.getTo();

        return null;
    }

    public FlowGraphEdge getExitEdge() {
        Iterator ite = _outputEdges.iterator();
        while(ite.hasNext()) {
            FlowGraphEdge edge = (FlowGraphEdge) ite.next();
            LoopEdge le = (LoopEdge) edge.getEdgeInfo();
            if(le.getType() == EdgeInfo.LOOP_EXIT) {
                return edge;
            }
        }
        return null;
    }

    public FlowGraphNode getExitNode() {
        FlowGraphEdge edge = getExitEdge();
        if(edge != null) return edge.getTo();

        return null;
    }

    public String toString() {
        return new String("while(" +  ")");
    }
    
    public void setWhileEnd(WhileEndNode node) {
        _whileEnd = node;
    }
    
    public WhileEndNode getWhileEnd() {
        return _whileEnd;
    }
    
    public AST getCondition() {
        return _condition;
    }

    public void setConditionLine(Line l) {
        _conditionLine = l;
    }
    
    public Line getConditionLine() {
        return _conditionLine;
    }

    public void setEndLine(Line l) {
        _endLine = l;
    }

    public Line getEndLine() {
        return _endLine;
    }

    public StatementInfo getStatementInfo() {
        return _statementInfo;
    }

    public void addIteExtractedNode(FlowGraphNode node) {
        _iteExtractedNodes.add(node);
    }

    public FlowGraphNode getIteExtractedNode(int pos) {
        return (FlowGraphNode) _iteExtractedNodes.get(pos);
    }

    public void setIterations(int ite) {
        _iterations = ite;
    }

    public int getIterations() {
        return _iterations;
    }

    ///////////////////////////////////////////////////////////////////
    ////                      private variables                    ////

    private AST _condition;

    private WhileEndNode _whileEnd;
    private Line _conditionLine;
    private Line _endLine;

    private StatementInfo _statementInfo;
    private LinkedList _iteExtractedNodes;

    private int _iterations;
}
